import SwiftUI

// SellDetail
// 转一转详情

struct SellDetail: View {
    @Environment(\.dismiss) private var dismiss
    
    @StateObject private var modifyStateRequester = BookModifyStateRequester()
    @StateObject private var bookTradeFetcher = TradeBookTradeFetcher()
    
    var book: Book_Sell
    
    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 10) {
                Text(book.tag)
                    .font(.title3)
                    .fontWeight(.light)
                    .foregroundColor(.accent)
                
                Text(book.bookName)
                    .font(.title)
                    .fontWeight(.light)
                
                Text(String(format: "¥%.2f", Double(book.requirePrice) / 100))
                    .font(.title2)
                    .fontWeight(.light)
                    .foregroundColor(.orange)
                
                Button {
                    modifyStateRequester.request(bookId: book.bookId, state: 2)
                } label: {
                    Spacer()
                    Text("完成交易")
                        .foregroundColor(.white)
                        .font(.title3)
                        .padding(.vertical, 10)
                    Spacer()
                }
                .background(.accent)
                .cornerRadius(8)
                
                Divider()
                
                Text("作者：" + book.author)
                    .font(.subheadline)
                     
                Text("课程：" + book.course)
                    .font(.subheadline)

                Divider()
                
                Text("简介")
                    .font(.title2)
                
                Text(book.description)
                
                Divider()
                
                Text("图片")
                    .font(.title2)
                
                LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3)) {
                    ForEach(book.imageId, id:\.self) { imageId in
                        let imageURL = URL(string: "http://62.234.45.120:1230/storage/bookImage/" + imageId + ".jpeg")!
                        GeometryReader { geo in
                            NavigationLink(destination:PhotoDetail(imageURL: imageURL)) {
                                AsyncImage(url: imageURL) {image in
                                    image
                                        .resizable()
                                } placeholder: {
                                    ProgressView()
                                }
                            }
                        }
                        .cornerRadius(8.0)
                        .aspectRatio(1, contentMode: .fit)
                    }
                }
                
                Divider()
                
                Text("交易意向")
                    .font(.title2)
                
                ForEach(bookTradeFetcher.publishedResult.collection, id: \.tradeId) { item in
                    SellDetailListRow(trade: item)
                }
            }
            .padding()
            .task {
                bookTradeFetcher.fetch(bookId: book.bookId)
            }
            .navigationTitle("详情")
            .navigationBarTitleDisplayMode(.inline)
            .onChange(of: modifyStateRequester.requestStatus) { oldValue, newValue in
                if(oldValue == .requesting && newValue == .finish) {
                    if(modifyStateRequester.publishedResult) {
                        dismiss()
                    }
                }
            }
        }
    }
}

#Preview {
    SellDetail(book: DefaultBook_Sell)
}
